<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="128px"
    >
      <el-form-item label="订单编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="主订单号" prop="mainOrderId">
        <el-input
          v-model="queryParams.mainOrderId"
          placeholder="请输入主订单号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="目的港" prop="unloadingPort">
        <el-input
          v-model="queryParams.unloadingPort"
          placeholder="请输入目的港"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['business:cargo-info:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="primary"
          plain
          @click="addViewPlan('create')"
          v-hasPermi="['business:cargo-info:addplan']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 下发计划
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
 <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
      <el-table-column label="客户公司" align="center" prop="companyName" />
      <el-table-column label="运单号" align="center" prop="mainOrderId" width="150" />
      <el-table-column label="订单号" align="center" prop="orderId" width="100" />
      <el-table-column label="目的港" align="center" prop="destinationCityName" />
      <el-table-column label="件数" align="center" prop="warehouseNumber" width="60"/>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="viewForm(scope.row)"
            v-hasPermi="['business:cargo-info:query']"
          >
            订单信息
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
 </ContentWrap>
    

  <!-- 表单弹窗：添加/修改 -->
  <CargoInfoView ref="CargoInfoViewRef"/>
</template>

<script setup lang="ts">
import { getIntDictOptions, getStrDictOptions, DICT_TYPE } from '@/utils/dict'
import download from '@/utils/download'
import { CargoInfoApi, CargoInfoVO } from '@/api/business/cargoinfo'
import CargoInfoView from './CargoInfoView.vue'
import { dateFormatter } from '@/utils/formatTime'
import {useUserStore} from '@/store/modules/user'

/** 货物资料 列表 */
defineOptions({ name: 'CargoInfo' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const userStore = useUserStore();

const loading = ref(true) // 列表的加载中
const list = ref<CargoInfoVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  orderId: undefined,
  mainOrderId: undefined,
  unloadingPort: undefined,
  planStatus:1
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const multipleSelection= ref([]);
const handleSelectionChange = (rows)=>{
  console.log(rows)
  multipleSelection.value = rows.map(it=>it.id);
}

const addViewPlan = ()=>{
  if(multipleSelection.value.length == 0){
    message.warning('请选择要下发的计划！');
    return;
  }
  message.confirm('确定下发所选计划？', '提示').then(res=>{
    CargoInfoApi.addPlan({
      ids: multipleSelection.value
    }).then(res=>{
      message.success("下发成功");
      getList();
    })
  })
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CargoInfoApi.getCargoInfoPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}


/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

const CargoInfoViewRef = ref();
const viewForm = (row)=>{
    CargoInfoViewRef.value.open(row.id, 'kefu');
}


/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
